package com.hm.controller;

import com.hm.pojo.HmAddress;
import com.hm.pojo.HmCart;
import com.hm.pojo.HmItem;
import com.hm.pojo.QueryVo;
import com.hm.serivce.HmAddressService;
import com.hm.serivce.HmCartService;
import com.hm.serivce.HmItemService;
import com.hm.utils.HmResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@Api(value = "加入购物车接口",tags = "加入购物车接口")
public class HmCartController {

    @Autowired
    private HmCartService hmCartService;

    @Autowired
    private HmItemService hmItemService;

    @Autowired
    private HmAddressService hmAddressService;

    /**
     * 购物车存在相同商品时，只对商品的数量-1
     * @param
     */
    @RequestMapping(value = "/updateMinusNum",method = RequestMethod.POST)
    @ApiOperation(value = "购物车商品数量-1")
    public HmResult updateMinusNum(String cartid){
        HmCart hmCart = hmCartService.ConfirmOrdes(cartid);
        if (hmCart.getNum()>1){
            hmCartService.updateNum(hmCart.getNum()-1,cartid);
            return HmResult.ok("购物车商品数量-1成功!");
        }else {
            return HmResult.build(500,"购物车商品为1不能再减了！");
        }

    }

    /**
     * 购物车存在相同商品时，只对商品的数量+1
     * @param
     */
    @RequestMapping(value = "/updateNum",method = RequestMethod.POST)
    @ApiOperation(value = "购物车商品数量+1")
    public HmResult updateNum(String cartid){
        HmCart hmCart = hmCartService.ConfirmOrdes(cartid);
        hmCartService.updateNum(hmCart.getNum()+1,cartid);
        return HmResult.ok("购物车商品数量+1成功!");
    }

    /**
     * 删除购物车
     * @param arr
     * @return
     */
    @RequestMapping(value = "/DeleteCart",method = RequestMethod.GET)
    @ApiOperation(value = "删除购物车")
    public HmResult DeleteCart(String[] arr){
        hmCartService.DeleteCart(arr);
        return HmResult.ok();
    }

    /**
     * 确认订单页面
     * @param arr
     * @return
     */
    @RequestMapping(value = "/ConfirmOrdes",method = RequestMethod.GET)
    @ApiOperation(value = "确认订单页面数据回显")
    public HmResult ConfirmOrdes( String[] arr, HttpSession session){
        session.setAttribute("cartid",arr);
        HmAddress hmAddress = hmAddressService.ShowDefaultAddress();
        List<HmCart> list = new ArrayList<>();
        for (String cartid : arr) {
            HmCart hmCart = hmCartService.ConfirmOrdes(cartid);
            list.add(hmCart);
        }
        QueryVo queryVo = new QueryVo();
        queryVo.setHmCart(list);
        queryVo.setHmAddress(hmAddress);
        return HmResult.ok(queryVo);
    }

    /**
     * 加入购物车
     * @param hmCart
     * @return
     */
    @RequestMapping(value = "/insertCart",method = RequestMethod.POST)
    @ApiOperation(value = "加入购物车")
    public HmResult insertCart(HmCart hmCart, long id, HttpServletRequest request){
        HmItem hmItem = hmItemService.ShowItemDetails(id);
        //hmCart.setCartid(UUID.randomUUID().toString().replace("-",""));
        hmCart.setCartid(String.valueOf(hmItem.getId()+"2019"));
        //pan
        hmCart = hmCartService.ConfirmOrdes(hmCart.getCartid());
        if (hmCart!=null){
            hmCartService.updateNum(hmCart.getNum()+1,hmCart.getCartid());
        }else {
            HmCart hmCart1 = new HmCart();
            hmCart1.setCartid(String.valueOf(hmItem.getId()+"2019"));
            hmCart1.setItem_id(hmItem.getId());
            hmCart1.setTitle(hmItem.getTitle());
            hmCart1.setNorm(hmItem.getNorm());
            hmCart1.setPrice(hmItem.getPrice());
            hmCart1.setNum(1);
            hmCart1.setPic_path(hmItem.getImage());
            //int userid = (int) request.getAttribute("userid");
            //System.out.println(userid+"----------------------用户id");
            hmCart1.setUserid(1);
            hmCartService.insertCart(hmCart1);
        }
        return HmResult.ok("加入购物车成功");
    }

    /**
     * 显示购物车列表
     * @param userid
     * @return
     */
    @RequestMapping(value = "/ShowCartList",method = RequestMethod.POST)
    @ApiOperation("显示购物车列表")
    public List<HmCart> ShowCartList(int userid,HttpServletRequest request){
        //userid = (int) request.getAttribute("userid");
        List<HmCart> list = hmCartService.ShowCartList(1);
        return list;
    }




}
